﻿using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

namespace Common.Util
{
    public class FString
    {
        #region Validate PhoneNumber
        /// <summary>
        /// Kiem tra chuoi co phai la Email hay khong
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsEmail(string str)
        {
            return Regex.IsMatch(str, "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$");
        }

        /// <summary>
        /// Kiem tra chuoi chi chua chu cai va so
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsLettersAndNumberOnly(string str)
        {
            return Regex.IsMatch(str, @"^[a-zA-Z0-9]+$");
        }

        /// <summary>
        /// Kiem tra chuoi co phai la dia chi IP khong
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsIpAddress(string str)
        {
            return Regex.IsMatch(str, "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
        }

        /// <summary>
        /// Kiem tra so dien thoai co dung dinh dang : 0xxxxxxxxx hoac 84xxxxxxxxx
        /// </summary>
        /// <param name="strPhone"> </param>
        /// <returns></returns>
        public static bool ValidatePhoneNumber(string strPhone)
        {
            var myRegex = new Regex(@"^(0|84)\d{9}$");
            Match match = myRegex.Match(strPhone);
            if (match.Success)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Kiem tra chuoi co ton tai cac ki tu dac biet nhu: <script>,~,!,@,#,$,%,^,&,*
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool ValidateText(string text)
        {
            var myRegex = new Regex(@"</script>|<script>|~|!|@|#|\$|%|\^|&|\*");
            Match match = myRegex.Match(text);
            if (match.Success)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region "Convert Phone Number"
        /// <summary>
        /// Convert the first 0 number to 84
        /// Ex: 0909123456 -> 84909123456
        /// </summary>
        /// <param name="phone">Phone number</param>
        /// <returns></returns>
        public static string FromZeroTo84(string phone)
        {
            if (phone.Length > 9 && phone.Substring(0, 1) == "0")
            {
                phone = "84" + phone.Substring(1);
            }
            return phone;
        }

        /// <summary>
        /// Convert the first 0 number to 84
        /// Ex: 84909123456 -> 0909123456
        /// </summary>
        /// <param name="phone">Phone number</param>
        /// <returns></returns>
        public static string From84ToZero(string phone)
        {
            if (phone.Length > 9 && phone.Substring(0, 2) == "84")
            {
                phone = "0" + phone.Substring(2);
            }
            if (phone.Length > 9 && phone.Substring(0, 3) == "+84")
            {
                phone = "0" + phone.Substring(3);
            }
            return phone;
        }
        #endregion

        #region Convert
        /// <summary>
        /// Chuyen tu so sang chuoi
        /// </summary>
        /// <param name="iLong"></param>
        /// <returns></returns>
        public static string Hexa(long iLong)
        {
            string hex = String.Format("{0:X}", iLong);
            return hex.Substring(hex.Length - 8, 8);
        }

        /// <summary>
        /// Use the current thread's culture info for conversion
        /// </summary>
        public static string ToTitleCase(string str)
        {
            var cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
            return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
        }

        /// <summary>
        /// Overload which uses the culture info with the specified name
        /// </summary>
        public static string ToTitleCase(string str, string cultureInfoName)
        {
            var cultureInfo = new CultureInfo(cultureInfoName);
            return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
        }

        /// <summary>
        /// Overload which uses the specified culture info
        /// </summary>
        public static string ToTitleCase(string str, CultureInfo cultureInfo)
        {
            return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
        }
        #endregion

        /// <summary>
        /// Lay ben trai mot so ky tu, xoa bot nhung tu dac biet
        /// </summary>
        /// <param name="input"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetStringvalue(string input, int length)
        {
            input = KillChar(input);
            return input.Length > length ? input.Substring(0, length) : input;
        }

        /// <summary>
        /// Xoa mot so tu dac biet
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string KillChar(string input)
        {
            input = input.Trim();
            return Regex.Replace(input, "select|update|drop|--|insert|delete|xp_|sysobjects|syscolumns|'|<script>|</script>", "");
        }

        /// <summary>
        /// Chuyen sang khong dau de lam Alias
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ConvertToUnSign(string s)
        {
            var regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string temp = s.Normalize(NormalizationForm.FormD);
            return regex.Replace(temp, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D').ToLower();
        }
    }
}
